# 完整测试脚本
# 运行所有测试：单元测试、API测试、集成测试

param(
    [switch]$SkipUnitTests,
    [switch]$SkipApiTests,
    [switch]$SkipIntegrationTests,
    [switch]$Verbose
)

$ErrorActionPreference = "Stop"

Write-Host "🧪 SmartCodeService 完整测试套件" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

$testResults = @{
    UnitTests = $null
    ApiTests = $null
    IntegrationTests = $null
}

# 1. 单元测试
if (-not $SkipUnitTests) {
    Write-Host "📝 运行单元测试..." -ForegroundColor Yellow
    try {
        $unitTestResult = mvn test -q
        if ($LASTEXITCODE -eq 0) {
            Write-Host "  ✅ 单元测试通过" -ForegroundColor Green
            $testResults.UnitTests = $true
        } else {
            Write-Host "  ❌ 单元测试失败" -ForegroundColor Red
            $testResults.UnitTests = $false
        }
    } catch {
        Write-Host "  ❌ 单元测试错误: $($_.Exception.Message)" -ForegroundColor Red
        $testResults.UnitTests = $false
    }
    Write-Host ""
} else {
    Write-Host "⏭️  跳过单元测试" -ForegroundColor Gray
    Write-Host ""
}

# 2. API测试
if (-not $SkipApiTests) {
    Write-Host "🔌 运行API测试..." -ForegroundColor Yellow
    try {
        $apiTestResult = & "$PSScriptRoot\test-api.ps1" -Verbose:$Verbose
        if ($LASTEXITCODE -eq 0) {
            Write-Host "  ✅ API测试通过" -ForegroundColor Green
            $testResults.ApiTests = $true
        } else {
            Write-Host "  ❌ API测试失败" -ForegroundColor Red
            $testResults.ApiTests = $false
        }
    } catch {
        Write-Host "  ❌ API测试错误: $($_.Exception.Message)" -ForegroundColor Red
        $testResults.ApiTests = $false
    }
    Write-Host ""
} else {
    Write-Host "⏭️  跳过API测试" -ForegroundColor Gray
    Write-Host ""
}

# 3. 集成测试
if (-not $SkipIntegrationTests) {
    Write-Host "🔗 运行集成测试..." -ForegroundColor Yellow
    try {
        $integrationTestResult = & "$PSScriptRoot\test-integration.ps1"
        if ($LASTEXITCODE -eq 0) {
            Write-Host "  ✅ 集成测试通过" -ForegroundColor Green
            $testResults.IntegrationTests = $true
        } else {
            Write-Host "  ❌ 集成测试失败" -ForegroundColor Red
            $testResults.IntegrationTests = $false
        }
    } catch {
        Write-Host "  ❌ 集成测试错误: $($_.Exception.Message)" -ForegroundColor Red
        $testResults.IntegrationTests = $false
    }
    Write-Host ""
} else {
    Write-Host "⏭️  跳过集成测试" -ForegroundColor Gray
    Write-Host ""
}

# 测试总结
Write-Host "📊 测试总结" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan

$totalTests = 0
$passedTests = 0

if ($testResults.UnitTests -ne $null) {
    $totalTests++
    if ($testResults.UnitTests) { $passedTests++ }
    Write-Host "单元测试: $(if ($testResults.UnitTests) { '✅ 通过' } else { '❌ 失败' })" -ForegroundColor $(if ($testResults.UnitTests) { "Green" } else { "Red" })
}

if ($testResults.ApiTests -ne $null) {
    $totalTests++
    if ($testResults.ApiTests) { $passedTests++ }
    Write-Host "API测试: $(if ($testResults.ApiTests) { '✅ 通过' } else { '❌ 失败' })" -ForegroundColor $(if ($testResults.ApiTests) { "Green" } else { "Red" })
}

if ($testResults.IntegrationTests -ne $null) {
    $totalTests++
    if ($testResults.IntegrationTests) { $passedTests++ }
    Write-Host "集成测试: $(if ($testResults.IntegrationTests) { '✅ 通过' } else { '❌ 失败' })" -ForegroundColor $(if ($testResults.IntegrationTests) { "Green" } else { "Red" })
}

Write-Host ""
Write-Host "总计: $passedTests / $totalTests 通过" -ForegroundColor $(if ($passedTests -eq $totalTests) { "Green" } else { "Yellow" })

$successRate = if ($totalTests -gt 0) { [math]::Round(($passedTests / $totalTests) * 100, 2) } else { 0 }
Write-Host "成功率: $successRate%" -ForegroundColor $(if ($successRate -ge 80) { "Green" } else { "Yellow" })

exit $(if ($passedTests -eq $totalTests) { 0 } else { 1 })

